<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends TXT_Controller{

    /**
     * _before 
     * 执行方法前执行
     * 
     * @access protected
     * @return boolean
     */
    protected function _before($method) {

        // 忽略检查登录的方法
        $ignores = array();

        if (!in_array($method, $ignores)) {
            $this->_checkLogin();
        }

        // 加载父类方法
        parent::_before($method);

        return true;
    }

    /**
     * index 
     * 主页索引页(首页)
     * 
     * @param integer $currPage 当前记录
     * @access public
     * @return void
     */
    public function index($currPage = 0) {

        $data = array(
            'title' => '账户管理',
            'tpl' => 'account/index',
        );

        $uType = $this->config->item('u_type');
        $uid = intval($this->session->userdata('uid'));
        $perPage = $this->config->item('global_perpage');

        // 获取账户信息
        $this->load->model('account_model');
        $accountInfo = $this->account_model->getAccountByUid($uid);
        $account = array(
            'u_name' => $accountInfo['u_name'],
            'u_type' => $uType[$accountInfo['u_type']],
            'u_company_name' => $accountInfo['u_company_name'],
            'a_total_amount' => $accountInfo['a_total_amount'],
            'a_balance_amount' => $accountInfo['a_balance_amount'],
        );
        $data['account'] = $account;

        // 分页信息
        $pageOptions = array(
            'base_url' => '/account/index',
            'curr_page' => $currPage,
            'per_page' => $perPage,
            'total_rows' => 0,
            'first_link' => '第一页',
            'last_link' => '最后一页',
        );

        // 获取账户日志列表
        $accountLogs = $this->account_model->listAccountLog($accountInfo['aid'], $pageOptions);
        $data['accountLogs'] = $accountLogs;

        // 类型
        $data['alType'] = $this->config->item('account_log_type');

        // 服务商
        $data['accountSp'] = $this->config->item('account_sp');

        // 状态
        $data['alStatus'] = $this->config->item('account_log_status');

        $this->load->library('pagination');
        $this->pagination->initialize($pageOptions);
        $data['page'] = $this->pagination->create_links();

        $this->_render('base/frame', $data);
    }

    /**
     * edit 
     * 编辑账户信息
     * 
     * @access public
     * @return void
     */
    public function edit() {

        $uid = intval($this->session->userdata('uid'));

        $this->load->model('user_model');
        $result = $this->user_model->editUser($uid);

        if ($result['result']) {
            return $this->_ajaxMessage(0, $result['message']);
        } else {
            return $this->_ajaxMessage(1, $result['message']);
        }
    }
}
